
# A2_plot_import_prices
#===============================================================================

# Description: This file plots average import prices for each country and creates 
# one of the Figure 1 subplots

rm(list = ls())

setwd("D:/data_replication")

library("data.table")  
library("ggplot2")  
library("ggrepel")  
library("stargazer")  

output <- fread('statistics_and_graphs/import_prices/output_files/prices_importer.txt')
output[ , V1 := gsub("s", "", V1)]
output <- output[V1 != ""]
output <- output[V1 != "_con"]
setnames(output, "V1", "declarant")
output[ , declarant := as.numeric(declarant)]
setnames(output, "V2", "price")
output[ , price := as.numeric(price)]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_declarant.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "declarant")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_declarant_codes.csv') 
output <- merge(output, iso3, by = "declarant")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_declarant)]

plot_import_prices <- ggplot(output, aes(x=gdp_log, y=price)) +
                geom_text(label=output$declarant_description, size = 4.9) +
                theme(text = element_text(size=17)) +
                geom_smooth(method=lm) +
                labs(x = "Importer GDP per capita (in logs)", y = "Avg. Import Price") +
                theme(axis.title=element_text(size=30)) +
                theme(axis.text=element_text(size=18))

ggsave("statistics_and_graphs/import_prices/plot_import_prices.png", plot = plot_import_prices, width = 12, height = 9)


